package com.feijin.testwisdomzonedemo.util.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.feijin.testwisdomzonedemo.R;
import com.lgc.garylianglib.util.data.DensityUtil;
import com.lgc.garylianglib.util.data.ResUtil;

/**
 * <pre>
 *     author : feijin_lgc
 *     e-mail : 595184932@qq.com
 *     time   : 2018/4/24 下午3:18
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class SimpleDialogUtil {

    private static Dialog dialog;

    public static void showSimpleDialog(Context context, int stringId, final OnClickCancelListener onClickCancelListener, final OnClickConfirmListener onClickConfirmListener) {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            dialog = null;
        }
        dialog = new Dialog(context, R.style.NormalDialogStyle);
        View view = View.inflate(context, R.layout.dialog_default_layout, null);
        TextView cancel = view.findViewById(R.id.cancel);
        TextView confirm = view.findViewById(R.id.confirm);
        TextView content = view.findViewById(R.id.tv_tip_content);
        content.setText(ResUtil.getString(stringId));
        dialog.setContentView(view);
        //使得点击对话框外部不消失对话框
        dialog.setCanceledOnTouchOutside(true);
        //设置对话框的大小
        view.setMinimumHeight((int) (DensityUtil.getScreenHeight(context) * 0.23f));
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (DensityUtil.getScreenWidth(context) * 0.75f);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickCancelListener != null) {
                    onClickCancelListener.onClickCancalListener(v);
                }
                dialog.dismiss();
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickConfirmListener != null) {
                    onClickConfirmListener.onClickConfirmListener(v);
                }
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    public static void showSimpleDialog(Context context, String title, String content, final OnClickCancelListener onClickCancelListener, final OnClickConfirmListener onClickConfirmListener) {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            dialog = null;
        }
        dialog = new Dialog(context, R.style.NormalDialogStyle);
        View view = View.inflate(context, R.layout.dialog_default_layout, null);
        TextView cancel = view.findViewById(R.id.cancel);
        TextView confirm = view.findViewById(R.id.confirm);

        TextView tv_tip = view.findViewById(R.id.tv_tip);
        TextView contentTv = view.findViewById(R.id.tv_tip_content);
        if (title.isEmpty()) {
            tv_tip.setVisibility(View.GONE);
        } else {

            tv_tip.setText(title);
        }
        if (content.isEmpty()) {
            contentTv.setVisibility(View.GONE);
        } else {

            contentTv.setText(content);
        }
        dialog.setContentView(view);
        //使得点击对话框外部不消失对话框
        dialog.setCanceledOnTouchOutside(true);
        //设置对话框的大小
        view.setMinimumHeight((int) (DensityUtil.getScreenHeight(context) * 0.23f));
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (DensityUtil.getScreenWidth(context) * 0.75f);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);


        if (onClickCancelListener != null) {
            cancel.setVisibility(View.VISIBLE);
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickCancelListener.onClickCancalListener(v);
                    dialog.dismiss();
                }
            });
        } else {

            cancel.setVisibility(View.GONE);
        }
        if (onClickConfirmListener != null) {
            confirm.setVisibility(View.VISIBLE);
            confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    onClickConfirmListener.onClickConfirmListener(v);
                    dialog.dismiss();
                }
            });
        } else {

            confirm.setVisibility(View.GONE);
        }

        dialog.show();
    }

    public static void disMissDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    public interface OnClickConfirmListener {
        void onClickConfirmListener(View view);
    }

    public interface OnClickCancelListener {
        void onClickCancalListener(View view);
    }
}
